/**
 * @file WinSerialReader.hh
 * @author Bono.ASUN.TamGitsun (gitsun.tam@outlook.com)
 * @brief 
 * @version 0.1
 * @date 2022-02-06
 * 
 * @copyright Copyright (c) 2022
 * 
 */


#pragma once


#include "Reader.hh"
#include <thread>
#include <mutex>
#include <windows.h>


namespace TGS::readers {


class WinSerialReader: public Reader
{
public:
    static const std::string PROPERTY_DEVICE;
    static const std::string PROPERTY_BAUD_RATE;
    static const std::string PROPERTY_BYTE_SIZE;
    static const std::string PROPERTY_PARITY;
    static const std::string PROPERTY_STOP_BITS;
public:
    void set_property(const std::string & key, const std::string & value);
    bool open(void);
    bool close(void);
    bool reset(void);
    int32_t send(uint8_t * data, int32_t size);
    int32_t read(uint8_t * buffer, int32_t size);
    int32_t available(void);
    virtual ~WinSerialReader(void);
private:
    static void read_thread(WinSerialReader * reader);
    int32_t available_fl(void);
private:
    HANDLE mCommHandle {INVALID_HANDLE_VALUE};
    std::thread mThread;
    std::mutex mReadMutex;
    uint8_t mReadData[2048];
    uint8_t * mReadPtrR {mReadData};
    uint8_t * mReadPtrW {mReadData};
    bool mRun {false};
    bool mHasReadData {false};
};


}
